package com.woniu.cms.goods.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.cms.goods.core.entity.GoodsStores;
import com.woniu.cms.goods.core.entity.GoodsSupplier;
import com.woniu.cms.goods.core.mapper.GoodsSupplierMapper;
import com.woniu.cms.goods.core.service.IGoodsSupplierService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.api.Result;
import com.woniu.common.api.status.Impl.BusinessCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 物资供应商表 服务实现类
 * </p>
 *
 * @author romantic sheep
 * @since 2022-09-17
 */
@Service
public class GoodsSupplierServiceImpl extends ServiceImpl<GoodsSupplierMapper, GoodsSupplier> implements IGoodsSupplierService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ObjectMapper objectMapper;
    /*
     * 物资供应商分页查询方法
     * @currentPage 当前页码
     * @pageSize 页面大小
     * @storeName 用户输入的仓库名称
     * */
    @Override
    public Result getlist(Integer currentPage, Integer pageSize, String supplierName,Integer status) {
        IPage<GoodsSupplier> iPage =new Page(currentPage,pageSize);
        LambdaQueryWrapper<GoodsSupplier> qw = Wrappers.lambdaQuery();
        if(StringUtils.hasText(supplierName)){
            qw.like(GoodsSupplier::getSupName,supplierName);
        }
        if (status!=null){
            qw.eq(GoodsSupplier::getStatus,status);
        }
        IPage<GoodsSupplier> page = this.page(iPage,qw);
        long total = page.getTotal();
        List<GoodsSupplier> records = page.getRecords();
        Map map=new HashMap();
        map.put("total",total);
        map.put("data",records);
        Result<Map> result = Result.common(map);
        return result;

    }
    /*
     * 新增物资供应商的方法
     * @stores 用户输入的供应商对象的
     * */
    @Override
    public Result addSupplier(@RequestBody GoodsSupplier goodsSupplier) throws JsonProcessingException {
        if (ObjectUtils.isEmpty(goodsSupplier)){
            return Result.error(BusinessCode.ADD_FAIL);
        }
        LambdaQueryWrapper<GoodsSupplier> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(GoodsSupplier::getSupName,goodsSupplier.getSupName());
        GoodsSupplier one = this.getOne(queryWrapper);
        if (!ObjectUtils.isEmpty(one)){
            return Result.error("供应商已存在");
        }
        boolean save = this.save(goodsSupplier);
        if (!save){
            return Result.error("供应商已存在");
        }
        return Result.ok(BusinessCode.ADD_SUCCESS);
    }
    /*
     * 删除供应商的方法，只能删除未提交或已驳回的单子。当供应商有被1条或多条合同引用时无法删除
     * @id 供应商id
     * */
    @Override
    public Result deleteSupplier(Integer id) {
        GoodsSupplier supplier = this.getById(id);
        if (supplier.getStatus()!=0&&supplier.getStatus()!=3){
            return Result.error("单据已提交无法删除");
        }
        boolean b = this.removeById(id);
        if (!b){
            return Result.error(BusinessCode.REMOVE_FAIL);
        }
        return Result.error(BusinessCode.REMOVE_SUCCESS);
    }
    /*
     * 修改供应商的方法，只能修改未提交、已驳回的单子。当供应商有被1条或多条合同引用时无法删除
     * @goodsSupplier 修改的供应商对象
     * */
    @Override
    public Result updateSupplier(@RequestBody GoodsSupplier goodsSupplier) {
        if (goodsSupplier.getStatus()!=0&&goodsSupplier.getStatus()!=3){
            return Result.error("单据已提交无法修改");
        }
        boolean b = this.updateById(goodsSupplier);
        if (!b){
            return Result.error(BusinessCode.MODIFY_FAIL);
        }
        return Result.ok(BusinessCode.MODIFY_SUCCESS);
    }
}
